package com.moecollege.moecollegelib.network;

import android.app.DownloadManager;
import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.Map;


/**
 * Created by nieyonghui on 15-5-27.
 */
public class NetWorkHelper {
    private static RequestQueue mQueue;

    public static void init(Context context) {
        mQueue = Volley.newRequestQueue(context);
    }
    public interface Method {
        int DEPRECATED_GET_OR_POST = -1;
        int GET = 0;
        int POST = 1;
        int PUT = 2;
        int DELETE = 3;
        int HEAD = 4;
        int OPTIONS = 5;
        int TRACE = 6;
        int PATCH = 7;
    }

    /**
     * @param method
     * @param url
     * @param listener
     * @param body
     */
    public void stringRequest(int method, String url, final RequestListener listener, final Map<String, String> body) {

        StringRequest request = new StringRequest(method, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                listener.onResponse(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.onError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                if (body != null) {
                    return body;
                }
                return super.getParams();
            }
        };
        mQueue.add(request);
    }
}
